﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Suagiaovien : System.Web.UI.Page
{
    BUS_Teacher objT = new BUS_Teacher();
    
    protected void Page_Load(object sender, EventArgs e)
    {

        string strcid = "", strpid = "";
        Page.Title = "Sửa Thông Tin Giáo Viên";
        if (!IsPostBack)
        {
            if (Session["AdminLogin"] != null)
            {
                strcid = Request.QueryString.Get("cid");
                strpid = Request.QueryString.Get("id");
                if (strcid == null || strpid == null)
                {
                    Response.Redirect("Giaovien.aspx");
                }
                else
                {
                    LoadData(Convert.ToInt16(Request.QueryString.Get("cid")), Convert.ToInt16(Request.QueryString.Get("id")));
                }
            }
            else Response.Redirect("Dangnhap.aspx");
        }
    }
    public void LoadData(int cgid, int pid)
    {
        try
        {
            var dataT = objT.Teacher_getAllByPid(pid);     
            int status = int.Parse(dataT.Rows[0]["Status"].ToString());
            string gioitinh = dataT.Rows[0]["Gentle"].ToString();
            DateTime ngay;
            linkxemgv.NavigateUrl = "Xemgiaovien.aspx?cid=" + cgid + "&id=" + pid;
            linksuagv.NavigateUrl = "Suagiaovien.aspx?cid=" + cgid + "&id=" + pid;
            linkthemgv.NavigateUrl = "Themgiaovien.aspx";
            linkthemgv.NavigateUrl = "Timgiaovien.aspx";
            
            magv.Text = dataT.Rows[0]["Teacherid"].ToString();
            tengv.Text = dataT.Rows[0]["Fullname"].ToString();
            dropgt.Text = gioitinh;
            
            ngay = Convert.ToDateTime(dataT.Rows[0]["Birthday"].ToString());
            ns.Text = ngay.ToString("dd/MM/yyyy");
            dc.Text = dataT.Rows[0]["Adress"].ToString();
            sdt.Text = dataT.Rows[0]["Phone"].ToString();
            hinhanhdaidien.ImageUrl = "~/Uploads/Teacher/" + dataT.Rows[0]["Photo"].ToString();
            ////load du lieu len dropdownlist loai san pham

            if (status == 1)
            {
                droptrangthai.SelectedIndex = 0;
            }
            else droptrangthai.SelectedIndex = 1;
        }
        catch
        {
        }

    }
    protected void btncapnhatgv_Click(object sender, EventArgs e)
    {
        try
        {
            DTO_Teacher dtoT = new DTO_Teacher();
            int cid = Convert.ToInt16(Request.QueryString.Get("cid"));
            int pid = Convert.ToInt16(Request.QueryString.Get("id"));
            dtoT.ID = pid;
            if (magv.Text != "" && tengv.Text != "")
            {
                dtoT.Teacherid = magv.Text.Trim();
                dtoT.Fullname = tengv.Text.Trim();
                dtoT.Gentle = dropgt.SelectedValue.ToString();
                DateTime strngay = DateTime.Parse(ns.Text.Trim());
                dtoT.Birthday = strngay.ToString("MM/dd/yyyy");
                dtoT.Adress = dc.Text.Trim();
                dtoT.Phone = sdt.Text.Trim();
                dtoT.Status = int.Parse(droptrangthai.SelectedValue.ToString());

                if (uploadanhgv.FileName != string.Empty)
                {
                    Session["isChangePic"] = true;
                    dtoT.Photo = uploadanhgv.FileName.Substring(uploadanhgv.FileName.LastIndexOf(@"\") + 1);
                    if (!UploadImage(dtoT.Photo))
                        return;
                }
                else
                {
                    Session["isChangePic"] = false;
                }

                

                if (!(bool)Session["isChangePic"])
                {
                    dtoT.Photo = hinhanhdaidien.ImageUrl.Substring(hinhanhdaidien.ImageUrl.LastIndexOf("/") + 1);
                }
                //if (!(bool)Session["isChangePic1"])
                //{
                //    dtoP.Image = hinhanhtintuc.ImageUrl.Substring(hinhanhtintuc.ImageUrl.LastIndexOf("/") + 1);
                //}
                if (objT.Teacher_Update(dtoT))
                {
                    Response.Write("<script>alert(\"Cập nhật thông tin thành công <..*!>\")</script>");
                    Response.Redirect("Xemgiaovien.aspx?cid=" + cid + "&id=" + pid);
                }
                else Response.Write("<script>alert(\"Cập nhật không thành công <..*!>\")</script>");
            }
            else
            {
                Response.Write("<script>alert(\"Vui lòng điền đầy đủ thông tin có (*)\")</script>");
            }

        }
        catch (Exception ex)
        {

            Response.Write("<script>alert(\"Lỗi " + ex.Message + " <..*!>\")</script>");
        }

    }

    //Phuong thuc upload anh
    public bool UploadImage(string imagelargename)
    {
        bool complete = false;
        //Lay ve ten file anh
        string fname = uploadanhgv.FileName;
        //Tach ten file
        string[] tokens = fname.Split(new char[] { '.' });
        //Luu tru dung luong file tinh bang byte
        int bytes = uploadanhgv.FileBytes.Length;
        //Kiem tra dung luong file anh
        if (bytes > (1024 * 1024))
        {
            lblargeerr.Text = "Dung lượng ảnh phải nhỏ hơn 4KB";
            return false;
        }
        //Kiem tra dang file upload
        if (tokens[tokens.Length - 1] == "jpg" || tokens[tokens.Length - 1] == "jpeg" || tokens[tokens.Length - 1] == "png" || tokens[tokens.Length - 1] == "gif")
        {
            //Khai bao bien luu tru duong dan den thu muc images
            string relPath = Server.MapPath("..\\.");
            //Khai bao bien luu tru duong dan tuyet doi
            string absolutePath = relPath + "\\Uploads\\Teacher\\" + imagelargename;
            //Luu len server
            uploadanhgv.SaveAs(absolutePath);
            complete = true;
        }
        else
        {
            lblargeerr.Text = "Đây không phải file ảnh.";
            return false;
        }
        return complete;
    }
}